﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using MoneyBookerDB;
using System.Windows.Forms;
using System.Net;
using System.Xml;
using System.Drawing;


namespace MoneyBooker {
	class CsvParser {

		MainWindow mMainWindow;
		Account mMainAccount;
		MoneyDBEntity mDb;

		List<string> mInputList = new List<string>();

		public CsvParser( MainWindow mainWindow ) {
			mMainWindow = mainWindow;
		}

		//public void EventH

		public void parseFile( string filename, out int totalLines, out int newLines ) {
			mDb = new MoneyDBEntity();

			mInputList.Clear();

			TextReader reader = new StreamReader( filename );
			string line;

			newLines = 0;
			totalLines = 0;

			while ((line = reader.ReadLine()) != null) {
				line = line.Trim();
				if (!line.StartsWith( "Rekening" ) && line != string.Empty) {
					++totalLines;
					List<TransactionLog> tranLog = (from tran in mDb.TransactionLog
												   where tran.transaction == line
												   select tran).ToList();

					if (tranLog.Count == 0) {
						mInputList.Add( line );
						TransactionLog newLog = new TransactionLog();
						newLog.transaction = line;

						mDb.TransactionLog.AddObject( newLog );
						++newLines;
					}
				}
			}

			mDb.SaveChanges();

			mDb.Dispose();
		}

		public void parseTransactions() {
		}

		//int getBank( string accountNr, MoneyDBEntity db ) {
		//    string bankBic = getBankBic( accountNr );
		//    List<Bank> banks = db.Banks.Where( b => b.bic == bankBic ).ToList();

		//    if (banks.Count == 0) {
		//        string bankName = getBankName( accountNr );
		//        Image bankLogo = getBankLogo( accountNr );
		//        Bank bank = new Bank();
		//        bank.bic = bankBic;
		//        bank.lastUpdate = DateTime.Now;
		//        MemoryStream mem = new MemoryStream();
		//        bankLogo.Save( mem, System.Drawing.Imaging.ImageFormat.Jpeg );
		//        bank.logo = mem.ToArray();
		//        bank.name = bankName;

		//        db.AddToBanks( bank );
		//        saveChanges( db );
		//        banks.Add( bank );
		//    }

		//    return banks[0].id;
		//}

		//int getAccount( string accountNr, int bankId, MoneyDBEntity db, bool mainAccount = false ) {
		//    List<Account> accounts = db.Accounts.Where( ac => ac.iban == accountNr ).ToList();
		//    if (accounts.Count == 0) {
		//        Account account = new Account();
		//        account.iban = accountNr;
		//        account.bankId = bankId;
		//        db.AddToAccounts( account );
		//        db.SaveChanges();
		//        accounts.Add( account );
		//    }
		//    if (mainAccount) {
		//        mMainAccount = accounts[0];
		//    }
		//    return accounts[0].id;
		//}

		//void addTransfer( int bankId, int accountId, string date, string description,
			//string valuta, string amount, MoneyDBEntity db ) {

			//description = description.Replace( '\"', ' ' ).Trim();
			//int account2 = 0;

			//if (description.StartsWith( "OVERSCHRIJVING VAN" )) {
			//    description = description.Remove( 0, 19 );
			//    string accountNr = description.Substring( 0, 14 );
			//    int bankId2 = getBank( accountNr, db );
			//    account2 = getAccount( accountNr, bankId2, db );
			//    description = description.Remove( 0, 15 );
			//}
			//else if (description.StartsWith( "EUROPESE OVERSCHRIJVING NAAR" )) {
			//    description = description.Remove( 0, 29 );
			//    string accountNr = description.Substring( 0, 19 );
			//    int bankId2 = getBank( accountNr, db );
			//    account2 = getAccount( accountNr, bankId2, db );
			//    description = description.Remove( 0, 20 );
			//}

			//double money = double.Parse( amount );

			//Transfer transfer = new Transfer();

			//if (money > 0) {
			//    if (account2 != 0) {
			//        transfer.from = account2;
			//    }
			//    transfer.to = accountId;
			//}
			//else {
			//    transfer.from = accountId;
			//    if (account2 != 0) {
			//        transfer.to = account2;
			//    }
			//}

			//mMainAccount.balance += money;

			//transfer.amount = money;
			//transfer.date = DateTime.Parse( date );
			//transfer.valuta = DateTime.Parse( valuta );
			//transfer.message = description;

			//db.AddToTransfers( transfer );
		//}

		//string getBankName( string account ) {
		//    string reqStr = "http://www.ibanbic.be/IBANBIC.asmx/BBANtoBANKNAME?Value=" + account;
		//    WebRequest request = WebRequest.Create( reqStr );
		//    Stream response = request.GetResponse().GetResponseStream();
		//    XmlDocument doc = new XmlDocument();
		//    doc.Load( response );

		//    return doc.DocumentElement.InnerText;
		//}

		//string getBankBic( string account ) {
		//    string reqStr = "http://www.ibanbic.be/IBANBIC.asmx/BBANtoBIC?Value=" + account;
		//    WebRequest request = WebRequest.Create( reqStr );
		//    Stream response = request.GetResponse().GetResponseStream();
		//    XmlDocument doc = new XmlDocument();
		//    doc.Load( response );

		//    return doc.DocumentElement.InnerText;
		//}

		//Image getBankLogo( string account ) {
		//    string reqStr = "http://www.ibanbic.be/logo.aspx?c=" + account;
		//    WebClient client = new WebClient();
		//    Stream stream = client.OpenRead( reqStr );
		//    Image bitmap = new Bitmap( stream );
		//    stream.Flush();
		//    stream.Close();

		//    return bitmap;
		//}

		//int countLines( string fileName ) {
		//    TextReader reader = new StreamReader( fileName );
		//    int numLines = 0;
		//    while (reader.ReadLine() != null) {
		//        ++numLines;
		//    }
		//    return numLines;
		//}

		//void saveChanges( MoneyDBEntity db ) {
		//    try {
		//        db.SaveChanges();
		//    }
		//    catch (Exception ex) {
		//        while (ex.InnerException != null) {
		//            ex = ex.InnerException;
		//        }
		//        MessageBox.Show( "An error occured:\n" + ex.Message );
		//    }
		//}
	}
}
